package com.example.yuan0605.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.yml.R;

import java.util.List;

public class MyPageAdapter extends BaseAdapter {
    List<User> list;
    Context context;

    public MyPageAdapter(List<User> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        TextView name;
        TextView pw;
        TextView sex;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_2, null);
            viewHolder = new ViewHolder();
            viewHolder.name = convertView.findViewById(R.id.name);
            viewHolder.pw = convertView.findViewById(R.id.pw);
            viewHolder.sex = convertView.findViewById(R.id.sex);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        User user = list.get(position);
        viewHolder.name.setText(user.getUsername());
        viewHolder.pw.setText(user.getPassword());
        viewHolder.sex.setText(user.getSex());
        return convertView;
    }
}
